﻿
$.CFormat = {
    Culture: function () {
        return $.getCookie("iCMS:Culture") || "en-US";
    },
    ParseFloat: function (value, culture) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            ret = $.parseFloat(value, "n", culture);
        }
        return ret;
    },
    ParseDate: function (value, culture) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (typeof value != "undefined" && value != "" && $.trim(value) != "undefined") {
            var temp = value.toString().split(" ");
            if (temp && temp.length > 0) {
                var date = temp[0];
                var time = temp[1] || "";
                var hour = 0, minute = 0, second = 0;
                if (time) {
                    var arrTime = time.split(":");
                    if (arrTime && arrTime.length > 0) {
                        hour = arrTime[0] || 0;
                        minute = arrTime[1] || 0;
                        second = arrTime[2] || 0;
                    }
                }
                var arrDate = date.split("/");
                if (date.indexOf("-") != -1)
                    arrDate = date.split("-");
                var year, month, day;

                if (arrDate && arrDate.length > 0) {
                    if (arrDate[0].length == 4) {
                        year = arrDate[0];
                        month = arrDate[1] - 1;
                        day = arrDate[2];
                    }
                    else {
                        var cdate = $.parseDate(date, "d", culture);
                        if (cdate) {
                            year = cdate.getFullYear();
                            month = cdate.getMonth();
                            day = cdate.getDate();
                        }
                    }
                }
                ret = new Date(year, month, day, hour, minute, second, 0);
            }
        }
        return ret;
    },
    Format: function (value, format, culture) {
        var ret = "";
        culture = culture || thisObj.Culture();
        ret = $.format(value, format, culture);
        return ret;
    },
    FormatNumber: function (value, format, culture, cultureTo) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            format = format || "n";
            if (typeof value != "number")
                value = thisObj.ParseFloat(value, culture);
            cultureTo = cultureTo || thisObj.Culture();
            ret = thisObj.Format(value, format, cultureTo);
        }
        return ret;
    },
    FormatDate: function (value, format, culture, cultureTo) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            format = format || "d";
            if (typeof value == "string")
                value = thisObj.ParseDate(value, culture);
            cultureTo = cultureTo || thisObj.Culture();
            ret = thisObj.Format(value, format, cultureTo);
        }
        return ret;
    },
    FormatDateTime: function (value, format, culture, cultureTo) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            format = format || "d";
            if (typeof value == "string")
                value = thisObj.ParseDate(value, culture);
            cultureTo = cultureTo || thisObj.Culture();

            ret = thisObj.Format(value, "d", cultureTo) + " " + thisObj.Format(value, "T", cultureTo);;
        }
        return ret;
    },
    InputFormatNumber: function (container) {
        this.ctrlDown = false;
        var self = this;
        this.handleKeyDown = function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key == 17) self.ctrlDown = true;
        };
        this.handleKeyUp = function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key == 17) self.ctrlDown = false;
        };
        this.ignoreEvent = function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key >= 16 && key <= 18) return true;
            if (key >= 33 && key <= 40) return true;
            if (self.ctrlDown && (key == 65 || key == 67)) return true;
            return false;
        };
        this.formatNumberUSD = function ($this, e) {
            if (!$this.val()) return;
            if ($this.val().match(/^.+?\.0?$/)) return;
            var culture = $.getCookie("iMarket:Session:Culture");
            var value = $.parseFloat($this.val(), "0", culture);
            var value_format = $.format(value, "n0", culture);
            $this.val(value_format);
        };

        $(container).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            self.handleKeyDown(e);
            return (
                      key == 8
                   || key == 9
                   || key == 46
                   || (key >= 35 && key <= 40)
                   || (key >= 48 && key <= 57 && event.shiftKey == false)
                   || (key >= 96 && key <= 105)
               );
        }).keyup(function (e) {
            var key = e.charCode || e.keyCode || 0;
            self.handleKeyUp(e);
            if (!self.ignoreEvent(e)) self.formatNumberUSD($(this), e);
        });
    }
};